data type and data structure in R

R
Author

Tony Duan

Published

July 6, 2023

Code
library(reticulate)

1 data type

1.1 character

recommend to use class() over typeof() to check data type

Code
x <- "dataset"
class(x)
[1] "character"

1.2 numeric

Code
x <- 123
class(x)
[1] "numeric"

1.3 complex

Code
x <- 3 + 2i
class(x)
[1] "complex"
Code
x <- TRUE
class(x)
[1] "logical"

2 Data structure

2.1 Vector

One number is a vector too.

Code
y <- 1
y
[1] 1
Code
class(y)
[1] "numeric"
Code
y <- c(1,2,3)
y
[1] 1 2 3
Code
class(y)
[1] "numeric"

create vector

Code
seq(from = 2, to = 14, by = 2) 
[1]  2  4  6  8 10 12 14
Code
rep(x = 1.5, times = 4)  
[1] 1.5 1.5 1.5 1.5

append vector

Code
x=c(1,2,3)
y=c(4,5,6)
z=c(x,y)
z
[1] 1 2 3 4 5 6

calculate vector

Code
x=c(1,2,3,4,5)
Code
sum(x)
[1] 15
Code
mean(x)
[1] 3
Code
y=x+10

select vector element

Code
y
[1] 11 12 13 14 15

select the first one

Code
y[1]
[1] 11
Code
# or 
head(y,1)
[1] 11

exclude the first one

Code
y[-1]
[1] 12 13 14 15
Code
y[length(y)]
[1] 15
Code
# or
tail(y,1)
[1] 15

select 3 to 5

Code
y[c(3:5)]
[1] 13 14 15

select bigger than 2 element

Code
y[y>2]
[1] 11 12 13 14 15

2.2 Matrix

Matrix is two dimensions data only with number.

Code
matrix001 <- matrix(1:16, nrow = 4)
matrix001
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
Code
is.matrix(matrix001)
[1] TRUE
Code
matrix002=matrix001+matrix001
matrix002
     [,1] [,2] [,3] [,4]
[1,]    2   10   18   26
[2,]    4   12   20   28
[3,]    6   14   22   30
[4,]    8   16   24   32

2.2.1 select one element in matrix first row and second column

Code
matrix002[1,2]
[1] 10

2.2.2 select second row

Code
matrix002[2,]
[1]  4 12 20 28

2.2.3 select third Column

Code
matrix002[,3]
[1] 18 20 22 24

2.3 Dataframe

Dataframe is two dimensions data mixed with number and string.

Code
height <- c(180, 155, 160, 167, 181)
weight <- c(65, 50, 52, 58, 70)
names <- c("Joanna", "Charlotte", "Helen", "Karen", "Amy")

y <- data.frame(height = height, weight = weight, names = names)
class(y)
[1] "data.frame"
Code
class(y$height)
[1] "numeric"
Code
class(y$names)
[1] "character"

2.4 Array

As array is made up matrices in multiple dimensions.

Code
# Create two vectors of different lengths.
vector1 <- c(9,1,0)
vector2 <- c(6,0,11,3,14,1,2,6,9)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))

array2
, , 1

     [,1] [,2] [,3]
[1,]    9    6    3
[2,]    1    0   14
[3,]    0   11    1

, , 2

     [,1] [,2] [,3]
[1,]    2    9    6
[2,]    6    1    0
[3,]    9    0   11

2.5 List

Code
y <- list(c("black", "yellow", "orange"),
               c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE),
               matrix(1:6, nrow = 3))
class(y)
[1] "list"

3 loop

3.1 for loop

Code
vector1=c(1,2,3)

for (i in vector1){
  print('number is:')
  print(i)
}
[1] "number is:"
[1] 1
[1] "number is:"
[1] 2
[1] "number is:"
[1] 3

3.2 while loop

Code
order=6

while (order>3){
  print(order)
  order=order-1
}
[1] 6
[1] 5
[1] 4

3.3 if loop

Code
a=16

if (a>10){
    print ("a>10")
}else{
  print("a<=10")
}
[1] "a>10"

3.4 if in for loop:

it will break out of the loop when i=2

Code
vector1=c(1,2,3)

for (i in vector1){
  print('number is:')
  if (i ==2){break}
  print(i)
}
[1] "number is:"
[1] 1
[1] "number is:"

3.5 if in while loop:

it will break out of the loop when order=4

Code
order=8

while (order>2){
  print(order)
  if (order ==4){break}
  order=order-1
}
[1] 8
[1] 7
[1] 6
[1] 5
[1] 4

4 Reference

http://adv-r.had.co.nz/Data-structures.html#vectors

https://yards.albert-rapp.de/

Loops using R programming: https://www.youtube.com/watch?v=UvopClD98LQ